package cn.bugstack.trigger.http;

import cn.bugstack.api.response.Response;
import cn.bugstack.types.enums.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RTopic;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author mikael wang
 * @date 2025/9/17 20:13
 */
@RestController("/api/v1/gbm/whitelist/")
@CrossOrigin("*")
@Slf4j
public class WhiteListController {

    @Resource
    private RTopic whiteListTopic;

    @RequestMapping(value = "update_config", method = RequestMethod.GET)
    public Response<Boolean> updateConfig(String userId) {
        try {
            log.info("白名单 动态配置值变更 userId:{}", userId);
            whiteListTopic.publish(userId);
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("白名单 动态配置值变更失败 userId:{}", userId, e);
            return Response.<Boolean>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
}
